package com.classicmodels.model;



import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

 

public class CustomerDAO {
     
    
   public static void save(Customer customer) throws Exception{
    Connection c = DataSource.getConnection();
    String cadenaSQL = "INSERT INTO  classic_models_inc.Customers(CustomerNumber,CustomerName,phone,contactFirstName,contactLastName," +
            "addressLine1, addressLine2,city,state,postalCode,country,salesRepEmployeeNumber, creditLimit)" +
            "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
   
    PreparedStatement stmt = (PreparedStatement) c.prepareStatement(cadenaSQL);
    
    
    stmt.setInt(1, customer.getCustomerNumber());
    stmt.setString(2, customer.getCustomerName());
    stmt.setString(3, customer.getPhone());
    stmt.setString(4, customer.getContactFirstName());
    stmt.setString(5, customer.getContactLastName());
    stmt.setString(6, customer.getAddressLine1());
    stmt.setString(7, customer.getAddressLine2());
    stmt.setString(8, customer.getCity());
    stmt.setString(9, customer.getState());
    stmt.setString(10, customer.getPostalCode());
    stmt.setString(11, customer.getCountry());
    stmt.setInt(12, customer.getSalesRepEmployeeNumber());
    stmt.setInt(13, customer.getCreditLimit());
    stmt.executeUpdate();
    
}   

    public static Customer[] all() throws SQLException, ClassNotFoundException {

        Connection c = DataSource.getConnection();
        java.sql.Statement s = c.createStatement();
        ResultSet result = (ResultSet) s.executeQuery("select * from Customers");
        ArrayList<Customer> customers = new ArrayList<Customer>();

        while(result.next()){

            Customer customer = new Customer();

            customer.setCustomerName(result.getString("customerName"));
            customer.setCustomerNumber(result.getInt("customerNumber"));
            customer.setPhone(result.getString("phone"));
            customer.setContactLastName(result.getString("ContactLastName"));
            customer.setContactFirstName(result.getString("ContactFirstName"));
            customer.setAddressLine1(result.getString("addressLine1"));
            customer.setAddressLine2(result.getString("addressLine2"));
            customer.setCity(result.getString("city"));
            customer.setState(result.getString("state"));
            customer.setPostalCode(result.getString("postalCode"));
            customer.setCountry(result.getString("country"));
            customer.setSalesRepEmployeeNumber(result.getInt("SalesRepEmployeeNumber"));
            customer.setCreditLimit(result.getInt("creditLimit"));
            customers.add(customer);
         }
        Customer[] customersArray = new Customer [customers.size()];
        //customers.toArray(customersArray);
         return customers.toArray(customersArray);

        // return customersArray;

    }








}

